<template>
	<view class="image" @click="handleUpload">
		<image v-if="!url" :src="config.baseUrl+bgImage"></image>
		<image v-else :src="url"></image>
	</view>
</template>

<script setup>
	import config from '@/config/config.js'
	import utils from '@/utils/util.js'
	const props = defineProps({
		bgImage: {
			type: String,
			default: ''
		},
		url: {
			type: String,
			default: ''
		}
	})
	const emits = defineEmits(['change'])
	const handleUpload = () => {
		uni.chooseImage({
			count: 1,
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths[0];
				uni.uploadFile({
					url: config.baseUrl + 'api/user/uploadFile', //仅为示例，非真实的接口地址
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						token: uni.getStorageSync('token')
					},
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data)
						if(res.status == 1) {
							emits('change', res.data.fullSrc)
						} else {
							utils.toast(res.info)
						}
					},
				})
			}
		});
	}
</script>

<style lang="scss" scoped>
	.image {
		width: 100%;
		height: 100%;

		image {
			width: 100%;
			height: 100%;
			border-radius: 10rpx;
		}
	}
</style>